#ifndef _SERVICE_H_
#define _SERVICE_H_

#include <sys/types.h>

/* impostazioni modificabili */
#define MAX_NAME_LEN    (16)
#define MAX_PATH_LEN    (128)
#define MAX_ARGS        (8)
/* il valore MAX_LINE e' approssimativo */
#define MAX_LINE        (1024)

/* costanti: non modificare */
#define NO_PID          (-1)
#define MAX_SRV_FIELDS  (5 + MAX_ARGS)


typedef enum {
  TCP, UDP
} transport_t;

typedef enum {
  WAIT, NOWAIT
} concurr_t;

struct service {
  char service_name[MAX_NAME_LEN+1];
  unsigned short port;
  transport_t transport;
  concurr_t concurr;
  char server_path[MAX_PATH_LEN+1];
  char* args[MAX_ARGS + 1];   // NULL terminated array of strings
  int sock;
  pid_t pid;
  struct service* prev;
  struct service* next;
};

typedef struct service service_t;


/* description: Build service list from file.
 * returns:     A pointer to head of the list, or NULL.
 */
service_t* parse_services(FILE* f);


/* description: Service string representation.
 * returns:     A short string to identify the service (for debug).
 */
char* service_to_str(const service_t* service);


/* description: Remove the element pointed by `service` from the list starting
 *   with the element pointed by `*head`.
 *   A double pointer is needed because calling this method could change the
 *   head of the list.
 */
void remove_service(service_t** head, service_t* service);


/* description: Service by pid list lookup.
 * returns:     A pointer to the first service with matching pid, or NULL.
 */
service_t* get_service_by_pid(service_t* head, pid_t pid);


/* description: Service by socket list lookup.
 * returns:     A pointer to the first service with socket matching `fd`, or NULL.
 */
service_t* get_service_by_sock(service_t* head, int fd);


#endif  /* _SERVICE_H_ */

